<?php
declare(strict_types=1);

namespace app\model;

use think\Model;

class Order extends Model
{
    protected $name = 'order';
    
    protected $autoWriteTimestamp = true;
    
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';

    // 订单状态
    const STATUS_UNPAID = 0;        // 未支付
    const STATUS_PAID = 1;          // 已支付待取件
    const STATUS_COLLECTING = 2;     // 收件中
    const STATUS_USER_CANCEL = 9;    // 用户取消
    const STATUS_COLLECTED = 10;     // 已取件
    const STATUS_COLLECT_FAIL = 11;  // 揽收失败
    const STATUS_RETURNED = 12;      // 已退回
    const STATUS_SIGNED = 13;        // 已签收
    const STATUS_SIGN_EXCEPTION = 14;// 异常签收
    const STATUS_SETTLED = 15;       // 已结算
    const STATUS_CANCELED = 99;      // 订单已取消

    // 支付方式
    const PAYMENT_WECHAT = 1;  // 微信支付
    const PAYMENT_ALIPAY = 2;  // 支付宝支付
    const PAYMENT_BALANCE = 3; // 余额支付

    // 货物类型
    const CARGO_TYPE_NORMAL = 1;    // 普通快递
    const CARGO_TYPE_FRAGILE = 2;   // 易碎品
    const CARGO_TYPE_FRESH = 3;     // 生鲜
    const CARGO_TYPE_DOCS = 4;      // 文件
    const CARGO_TYPE_CLOTHES = 5;   // 服装
    const CARGO_TYPE_DIGITAL = 6;   // 数码产品
    const CARGO_TYPE_FOOD = 7;      // 食品
    const CARGO_TYPE_OTHER = 99;    // 其他

    /**
     * 关联快递公司
     */
    public function expressCompany()
    {
        return $this->belongsTo(ExpressCompany::class, 'company_code', 'company_code');
    }

    /**
     * 关联快递100订单
     */
    public function kuaidi100Order()
    {
        return $this->hasOne(Kuaidi100Order::class, 'order_id', 'kuaidi100_order_id');
    }

    /**
     * 获取预约时间文本
     */
    public function getPickupTimeTextAttr(): string
    {
        if (!$this->pickup_date) {
            return '';
        }

        $timestamp = strtotime($this->pickup_date);
        $today = strtotime(date('Y-m-d'));
        
        if ($timestamp == $today) {
            $dateText = '今天';
        } elseif ($timestamp == strtotime('+1 day', $today)) {
            $dateText = '明天';
        } elseif ($timestamp == strtotime('+2 day', $today)) {
            $dateText = '后天';
        } else {
            $dateText = date('m月d日', $timestamp);
        }

        return $dateText . ' ' . $this->pickup_start_time . '-' . $this->pickup_end_time;
    }

    /**
     * 获取实际取件时间文本
     */
    public function getPickupActualTimeTextAttr(): string
    {
        return $this->pickup_actual_time ? date('m-d H:i', strtotime($this->pickup_actual_time)) : '';
    }

    /**
     * 获取订单状态文本
     */
    public function getOrderStatusTextAttr(): string
    {
        $statusMap = [
            self::STATUS_UNPAID => '待支付',
            self::STATUS_PAID => '待取件',
            self::STATUS_COLLECTING => '收件中',
            self::STATUS_USER_CANCEL => '已取消',
            self::STATUS_COLLECTED => '已取件',
            self::STATUS_COLLECT_FAIL => '揽收失败',
            self::STATUS_RETURNED => '已退回',
            self::STATUS_SIGNED => '已签收',
            self::STATUS_SIGN_EXCEPTION => '异常签收',
            self::STATUS_SETTLED => '已结算',
            self::STATUS_CANCELED => '已取消'
        ];
        
        return $statusMap[$this->order_status] ?? '未知状态';
    }

    /**
     * 获取支付方式文本
     */
    public function getPaymentMethodTextAttr(): string
    {
        $methodMap = [
            self::PAYMENT_WECHAT => '微信支付',
            self::PAYMENT_ALIPAY => '支付宝支付',
            self::PAYMENT_BALANCE => '余额支付'
        ];
        
        return $methodMap[$this->payment_method] ?? '未知方式';
    }

    /**
     * 获取货物类型文本
     */
    public function getCargoTypeTextAttr(): string
    {
        $typeMap = [
            self::CARGO_TYPE_NORMAL => '普通快递',
            self::CARGO_TYPE_FRAGILE => '易碎品',
            self::CARGO_TYPE_FRESH => '生鲜',
            self::CARGO_TYPE_DOCS => '文件',
            self::CARGO_TYPE_CLOTHES => '服装',
            self::CARGO_TYPE_DIGITAL => '数码产品',
            self::CARGO_TYPE_FOOD => '食品',
            self::CARGO_TYPE_OTHER => '其他'
        ];
        
        return $typeMap[$this->cargo_type] ?? '未知类型';
    }

    /**
     * 获取所有货物类型
     */
    public static function getCargoTypes(): array
    {
        return [
            [
                'value' => self::CARGO_TYPE_NORMAL,
                'label' => '普通快递',
                'desc' => '适用于一般物品',
                'allow_custom' => false
            ],
            [
                'value' => self::CARGO_TYPE_FRAGILE,
                'label' => '易碎品',
                'desc' => '玻璃制品、陶瓷等易碎物品',
                'allow_custom' => false
            ],
            [
                'value' => self::CARGO_TYPE_FRESH,
                'label' => '生鲜',
                'desc' => '需冷藏保鲜的物品',
                'allow_custom' => false
            ],
            [
                'value' => self::CARGO_TYPE_DOCS,
                'label' => '文件',
                'desc' => '文件、证件等',
                'allow_custom' => false
            ],
            [
                'value' => self::CARGO_TYPE_CLOTHES,
                'label' => '服装',
                'desc' => '衣物、布料等',
                'allow_custom' => false
            ],
            [
                'value' => self::CARGO_TYPE_DIGITAL,
                'label' => '数码产品',
                'desc' => '手机、电脑等电子产品',
                'allow_custom' => false
            ],
            [
                'value' => self::CARGO_TYPE_FOOD,
                'label' => '食品',
                'desc' => '零食、干货等常温食品',
                'allow_custom' => false
            ],
            [
                'value' => self::CARGO_TYPE_OTHER,
                'label' => '其他',
                'desc' => '请填写具体物品类型',
                'allow_custom' => true
            ]
        ];
    }

    /**
     * 订单列表数据处理
     */
    public function toList(): array
    {
        return [
            'id' => $this->id,
            'order_no' => $this->order_no,
            'order_status' => $this->order_status,
            'order_status_text' => $this->order_status_text,
            'express_company' => $this->expressCompany->company_name ?? '',
            'express_no' => $this->express_no,
            'fee_actual' => $this->fee_actual,
            'pickup_time_text' => $this->pickup_time_text,
            'pickup_actual_time_text' => $this->pickup_actual_time_text,
            'create_time' => date('Y-m-d H:i', strtotime($this->create_time))
        ];
    }

    /**
     * 订单详情数据处理
     */
    public function toDetail(): array
    {
        $detail = parent::toDetail();
        $detail['cargo_info'] = [
            'type' => $this->cargo_type,
            'type_text' => $this->cargo_type_text,
            'custom_type' => $this->cargo_type == self::CARGO_TYPE_OTHER ? $this->cargo_custom_type : '',
            'name' => $this->cargo_name,
            'quantity' => $this->cargo_quantity,
            'remark' => $this->cargo_remark
        ];
        return $detail;
    }
} 